/*
 * FindReplacePanel
 * Copyright (C) 2011, Aleksey Nikiforov
 *
 * This file is part of FindReplacePanel.
 *
 * FindReplacePanel is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FindReplacePanel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package simplex3d.console.findreplace;

import java.awt.event.ActionEvent;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;
import javax.swing.AbstractAction;
import javax.swing.JToggleButton;


/**
 *
 * @author Aleksey Nikiforov (lex)
 */
class ToggleAction extends AbstractAction {
    
    private Preferences preferences;
    private String key;


    public ToggleAction(
        String key, ResourceBundle bundle, String command,
        Preferences preferences, boolean defaultSelected
    ) {
        super(bundle.getString(key + ".text"));
        this.key = key;
        this.preferences = preferences;

        putValue(MNEMONIC_KEY, (int) bundle.getString(key + ".mnemonic").charAt(0));
        putValue(SHORT_DESCRIPTION, bundle.getString(key + ".tooltip"));
        putValue(ACTION_COMMAND_KEY, command);

        putValue(SELECTED_KEY, preferences.getBoolean(key + ".selected", defaultSelected));
    }

    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        if (src instanceof JToggleButton) {
            JToggleButton toggle = (JToggleButton) src;
            boolean selected = toggle.isSelected();
            putValue(SELECTED_KEY, selected);
            preferences.putBoolean(key + ".selected", selected);
        }
    }
}
